package com.yjlove.myplay.ui.adapter;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.yjlove.myplay.bean.BannerInfo;
import com.yjlove.myplay.utils.ImageFormatUtil;

import java.util.List;

/**
 * 作者 YJlvoe
 * 创建时间 2017/3/3.
 */

public class BannerAdapter extends PagerAdapter {

    private Activity mActivity;
    private List<BannerInfo> mDatas;

    public BannerAdapter(Activity activity, List<BannerInfo> mBannerInfos) {
        this.mActivity = activity;
        this.mDatas = mBannerInfos;
    }

    @Override
    public int getCount() {
        // 无限循环
        return  mDatas.size() * 1000 * 100;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        position = position % mDatas.size();// 无限循环中找对坐标
        ImageView imageView = new ImageView(mActivity);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        ImageLoader.getInstance().displayImage("drawable://" + mDatas.get(position).getRes(), imageView, ImageFormatUtil.getOptions());
        //imageView.setBackgroundResource(mDatas.get(position).getRes());
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //super.destroyItem(container, position, object);
        container.removeView((View) object);
    }
}


